<template>
  <!-- 消息配置修改-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="editForm" :model="editForm" :rules="rules" label-width="110px">
      <!--消息配置修改-->
      <el-card class="form-card msgConf-card">
        <div slot="header" class="label-header-msg">
          <span></span>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事件类型" prop="eventType">
              <el-select v-model="editForm.eventNm" placeholder="事件类型" style="width: 100%" :disabled="true">
                <el-option
                  v-for="dict in eventTypeList"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select v-model="editForm.isUse" placeholder="是否启用" style="width: 100%">
                <el-option
                  v-for="dict in dictMap.ON_OFF_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="节假日开关" prop="holidayFlag">
              <el-select v-model="editForm.holidayFlag" placeholder="节假日开关" style="width: 100%">
                <el-option
                  v-for="dict in dictMap.HOLIDAYFLAGCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发送开始时间" prop="sendStartDt">
              <el-time-picker style="width: 100%" v-model="editForm.sendStartDt" value-format="HHmmss" type="time" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送结束时间" prop="sendEndDt">
              <el-time-picker style="width: 100%" v-model="editForm.sendEndDt" value-format="HHmmss" type="time" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="通知类型" prop="sendType">
              <el-checkbox-group v-model="sendTypeList">
                <el-checkbox
                  v-for="dict in dictMap.MSG_SEND_TYPE"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                  @change="sendTypeChange"
                >
                  {{ dict.text }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每日执行次数" prop="execTime">
              <el-select v-model="editForm.execTime" placeholder="每日执行次数" style="width: 100%">
                <el-option
                  v-for="dict in dailyExecTimesList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件接收人小组" prop="mailRecGroup" v-if="mailRecGroupShow">
              <el-select multiple v-model="mailRecGroupList" placeholder="邮件接收人小组" style="width: 100%">
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件抄送人小组" prop="mailCopyGroup" v-if="mailCopyGroupShow">
              <el-select multiple v-model="mailCopyGroupList" placeholder="邮件抄送人小组" style="width: 100%">
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信接收人小组" prop="smsRecGroup" v-if="smsRecGroupShow">
              <el-select multiple v-model="smsRecGroupList" placeholder="短信接收人小组" style="width: 100%">
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件主题" prop="mailTopic" v-if="mailTopicShow">
              <el-input v-model="editForm.mailTopic" placeholder="邮件主题" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件内容" prop="mailContent" v-if="mailContentShow">
              <el-input v-model="editForm.mailContent" placeholder="邮件内容" type="textarea" autosize />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信内容" prop="smsContent" v-if="smsContentShow">
              <el-input v-model="editForm.smsContent" placeholder="短信内容" type="textarea" autosize />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { msgConfUpdate, msgConfDetail, getMonitorConfList, getMsgGroupList } from "@/api/cnaps/bizMng/msgConf.js";
import { listPageMixin } from "@/libs/listPageMixin";

export default {
  name: "msgConfEditDialog",
  mixins: [listPageMixin],
  components: {},
  data() {
    return {
      title: "",
      // 是否显示弹出层
      openState: false,
      mailRecGroupShow: false,
      mailCopyGroupShow: false,
      smsRecGroupShow: false,
      mailTopicShow: false,
      mailContentShow: false,
      smsContentShow: false,
      sendTypeList: [],
      eventTypeList: [],
      msgGroupList: [],
      mailRecGroupList: [],
      mailCopyGroupList: [],
      smsRecGroupList: [],
      dailyExecTimesList: [{value : 1, text : "单次"},{value : 2, text : "循环"}],
      // 字段可编辑控制

      //数据字典
      searchFormDictList: ["MSG_SEND_TYPE", "ON_OFF_STATUS", "HOLIDAYFLAGCODE"],
      // 表单参数
      editForm: {},
      // 表单校验
      rules: {
        eventType: [{ required: true, message: "请选择事件类型", trigger: "blur" }],
        isUse: [{ required: true, message: "请选择启用标识", trigger: "blur" }],
        holidayFlag: [{ required: true, message: "请选择节假日开关", trigger: "blur" }],
        sendStartDt: [{ required: true, message: "请选择发送开始时间", trigger: "blur" }],
        sendEndDt: [{ required: true, message: "请选择发送结束时间", trigger: "blur" }],
        sendType: [{ required: true, message: "请选择通知类型", trigger: "blur" }],
        mailRecGroup: [{ required: false, message: "请选择邮件接收人小组", trigger: "blur" }],
        mailCopyGroup: [{ required: false, message: "请选择邮件抄送人小组", trigger: "blur" }],
        smsRecGroup: [{ required: false, message: "请选择短信接收人小组", trigger: "blur" }],
        mailTopic: [{ required: false, message: "请输入邮件主题", trigger: "blur" }],
        mailContent: [{ required: false, message: "请输入邮件内容", trigger: "blur" }],
        smsContent: [{ required: false, message: "请输入短信内容", trigger: "blur" }],
      },
      opButtonList: () => {
        return [];
      },
    };
  },
  mounted() {
    this.initEventTypeList();
    this.initMsgGroupList();
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("editForm");
      this.$set(this, "editForm", {});
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.reset();
          done();
        })
        .catch((_) => {});
    },
    show(row) {
      this.title = "消息配置修改";
      this.openState = true;

      this.msgConfDetail(row);
    },

    msgConfDetail: function (row) {
      msgConfDetail(row).then((response) => {
        this.showData(response);
      });
    },

    showData: function (response) {
      this.editForm = response.result.data;
      this.initSendType(this.editForm);
      this.initMailRecGroup(this.editForm);
      this.initMailCopyGroup(this.editForm);
      this.initSmsRecGroup(this.editForm);
      this.sendTypeChange();
    },
    /** 初始化通知类型 */
    initSendType(row) {
      if (typeof row.sendType !== "undefined") {
        this.sendTypeList = row.sendType.split(",");
      }
    },

    /** 初始化邮件接收小组名称 */
    initMailRecGroup(row) {
      if (typeof row.mailRecGroup !== "undefined") {
        this.mailRecGroupList = row.mailRecGroup.split(",");
      }
    },

    /** 初始化邮件抄送小组名称 */
    initMailCopyGroup(row) {
      if (typeof row.mailCopyGroup !== "undefined") {
        this.mailCopyGroupList = row.mailCopyGroup.split(",");
      }
    },

    /** 初始化短信接收小组名称 */
    initSmsRecGroup(row) {
      if (typeof row.smsRecGroup !== "undefined") {
        this.smsRecGroupList = row.smsRecGroup.split(",");
      }
    },

    /** TODO提交按钮 */
    submitForm: function () {
      let mailRecGroup = "";
      this.mailRecGroupList.forEach((v) => {
        mailRecGroup = mailRecGroup + v + ",";
      });

      this.editForm.mailRecGroup = mailRecGroup.substring(0, mailRecGroup.length - 1);

      let mailCopyGroup = "";
      this.mailCopyGroupList.forEach((v) => {
        mailCopyGroup = mailCopyGroup + v + ",";
      });

      this.editForm.mailCopyGroup = mailCopyGroup.substring(0, mailCopyGroup.length - 1);

      let smsRecGroup = "";
      this.smsRecGroupList.forEach((v) => {
        smsRecGroup = smsRecGroup + v + ",";
      });

      this.editForm.smsRecGroup = smsRecGroup.substring(0, smsRecGroup.length - 1);

      let sendType = "";
      this.sendTypeList.forEach((v) => {
        sendType = sendType + v + ",";
      });

      this.editForm.sendType = sendType.substring(0, sendType.length - 1);
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.submit();
        }
      });
    },

    submit() {
      msgConfUpdate(this.editForm).then((res) => {
        if (res.code === "S") {
          this.msgSuccess("提交成功");
          this.cancel();
        }
      });
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", true);
    },

    //初始化获取监控事件列表
    initEventTypeList() {
      getMonitorConfList(this.editForm).then((res) => {
        if (res.code === "S") {
          const arr = [];
          const list = res.result || [];
          list.length &&
            list.forEach((v) => {
              arr.push({
                value: v.monitorId,
                text: v.eventNm,
              });
            });
          this.eventTypeList = arr;
        }
      });
    },

    //初始化获取消息分组
    initMsgGroupList() {
      getMsgGroupList(this.editForm).then((res) => {
        if (res.code === "S") {
          const arr = [];
          const list = res.result || [];
          list.length &&
            list.forEach((v) => {
              arr.push({
                value: v.groupId,
                text: v.groupNm,
              });
            });
          this.msgGroupList = arr;
        }
      });
    },

    sendTypeChange() {
      const sendType = this.sendTypeList;

      if ((sendType.indexOf("01") > -1) & (sendType.indexOf("02") > -1)) {
        //邮件通知和短信通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.rules.mailRecGroup = {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        };
        this.rules.mailCopyGroup = {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        };
        this.rules.mailTopic = {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        };
        this.rules.mailContent = {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        };
        this.rules.smsRecGroup = {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        };
        this.rules.smsContent = {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        };
      } else if ((sendType.indexOf("01") > -1) & (sendType.indexOf("02") < 0)) {
        //邮件通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.$set(this.editForm, "smsRecGroup", "");
        this.$set(this.editForm, "smsContent", "");

        this.rules.mailRecGroup = {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        };
        this.rules.mailCopyGroup = {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        };
        this.rules.mailTopic = {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        };
        this.rules.mailContent = {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        };
        this.rules.smsRecGroup = {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        };
        this.rules.smsContent = {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        };
      } else if ((sendType.indexOf("02") > -1) & (sendType.indexOf("01") < 0)) {
        //短信通知
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.$set(this.editForm, "mailRecGroup", "");
        this.$set(this.editForm, "mailCopyGroup", "");
        this.$set(this.editForm, "mailTopic", "");
        this.$set(this.editForm, "mailContent", "");

        this.rules.mailRecGroup = {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        };
        this.rules.mailCopyGroup = {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        };
        this.rules.mailTopic = {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        };
        this.rules.mailContent = {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        };
        this.rules.smsRecGroup = {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        };
        this.rules.smsContent = {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        };
      } else {
        //都不包含
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.$set(this.editForm, "mailRecGroup", "");
        this.$set(this.editForm, "mailCopyGroup", "");
        this.$set(this.editForm, "mailTopic", "");
        this.$set(this.editForm, "mailContent", "");
        this.$set(this.editForm, "smsRecGroup", "");
        this.$set(this.editForm, "smsContent", "");

        this.rules.mailRecGroup = {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        };
        this.rules.mailCopyGroup = {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        };
        this.rules.mailTopic = {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        };
        this.rules.mailContent = {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        };
        this.rules.smsRecGroup = {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        };
        this.rules.smsContent = {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        };
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
